<?php

namespace App\Http\Cache;

use App\Exceptions\Fail;
use Illuminate\Support\Facades\RateLimiter as Limiter;

class RateLimiter
{

    protected static int $maxAttempts = 5;
    protected static int $decaySeconds = 60 * 10;

    protected static string $preKey = 'RateLimiter';

    /**
     * @throws Fail
     */
    public static function check(string $key): void
    {
        if (Limiter::tooManyAttempts(self::$preKey . $key, self::$maxAttempts)) {
            throw new Fail('登录失败次数过多，请10分钟后再重试！！');
        }
    }

    public static function fail(string $key): void
    {
        Limiter::increment(self::$preKey . $key, self::$decaySeconds);
    }

    public static function clear(string $key): void
    {
        Limiter::clear(self::$preKey . $key);
    }
}
